<template>
	<div class="dashboard-container home">
		<!-- 营业数据 -->
		<Overview :overviewData="overviewData" />
		<!-- 订单管理 -->
		<Orderview :orderviewData="orderviewData" />
		<div class="homeMain">
			<!-- 菜品总览 -->
			<CuisineStatistics :dishesData="dishesData" />
			<!-- 套餐总览 -->
			<SetMealStatistics :setMealData="setMealData" />
		</div>
		<!-- 订单信息 -->
		<OrderList :order-statics="orderStatics" @getOrderListBy3Status="getOrderListBy3Status" />
	</div>
</template>

<script setup lang="ts">
import { onMounted, ref, watch } from 'vue';
import { ElMessage } from 'element-plus';
import pinia from '/@/stores/index';
import { useOrderStatus } from '/@/stores/orderStatus';
import {
	getBusinessData,
	getOrderData, //订单管理今日订单
	getOverviewDishes, //菜品总览
	getSetMealStatistics, //套餐总览
} from '/@/api/index';
import { getOrderListBy } from '/@/api/order';
// 组件
// 营业数据
import Overview from './src/components/overview.vue';
// 订单管理
import Orderview from './src/components/orderview.vue';
// 菜品总览
import CuisineStatistics from './src/components/cuisineStatistics.vue';
// 套餐总览
import SetMealStatistics from './src/components/setMealStatistics.vue';
// 订单列表
import OrderList from './src/components/orderList.vue';

let overviewData = ref({}) as any;
let orderviewData = ref({}) as any;
let dishesData = ref({}) as any;
let setMealData = ref({});
let orderStatics = ref({}) as any;

const storesOrderStatus = useOrderStatus(pinia);

onMounted(() => {
	getBusinessDataList();
	getOrderStatisticsData();
	getOverStatisticsData();
	getSetMealStatisticsData();
	getOrderListBy3Status();
});

// 获取营业数据
const getBusinessDataList = async () => {
	const res: any = await getBusinessData();
	if (res.code == 1) {
		overviewData.value = res.data;
	}
};

// 获取今日订单
const getOrderStatisticsData = async () => {
	const res = await getOrderData();
	if (res.code == 1) {
		orderviewData.value = res.data;
	}
};

// 获取菜品总览数据
const getOverStatisticsData = async () => {
	const res = await getOverviewDishes();
	if (res.code == 1) {
		dishesData.value = res.data;
	}
};

// 获取套餐总览数据
const getSetMealStatisticsData = async () => {
	const res = await getSetMealStatistics();
	if (res.code == 1) {
		setMealData.value = res.data;
	}
};

//获取待处理，待派送，派送中数量
const getOrderListBy3Status = () => {
  storesOrderStatus.setNewOrder(false)
	getOrderListBy({})
		.then((res: any) => {
			if (res.code === 1) {
				orderStatics.value = res.data;
			} else {
				ElMessage.error(res.msg);
			}
		})
		.catch((err: any) => {
			ElMessage.error('请求出错了：' + err.message);
		});

	//获取订单管理数据
	getOrderStatisticsData();
};

watch(
	() => storesOrderStatus.newOrder,
	() => {
		if (storesOrderStatus.newOrder) {
			getOrderStatisticsData();
			getOrderListBy3Status();
		}
	},
  {
    deep:true
  }
);

// watchEffect(() => {

// });
</script>

<style lang="scss" scoped>
@import url('./src/css/common.scss');
@import url('./src/css/dashboard.scss');
</style>
